package com.egreen.pointsmall.exception.handler;

import com.egreen.common.States;
import com.egreen.pointsmall.exception.PointsmallException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.ModelAndView;

@RestControllerAdvice
@Slf4j
public class PointMallExceptionHandler {
    /**
     * 积分商城处理异常
     */
    @ExceptionHandler(value = PointsmallException.class)
    public ModelAndView handlerException(PointsmallException e) {
        //进行异常的判断
        ModelAndView modelAndView = new ModelAndView();
        States states = e.getStates();
        switch (states.getStates()) {
            case 30001:
                modelAndView.setViewName("register");
                modelAndView.addObject("emailError", states.getDescription());
                break;
            case 30002:
                modelAndView.setViewName("register");
                modelAndView.addObject("mobileError", states.getDescription());
                break;
            case 30003:
                modelAndView.setViewName("register");
                modelAndView.addObject("userNameExists",  states.getDescription());
                break;
            case 30005:
                modelAndView.setViewName("login");
                modelAndView.addObject("userNameError",states.getDescription());
                break;
            case 30006:
                modelAndView.setViewName("login");
                modelAndView.addObject("passwordError",states.getDescription());
                break;
            case 30008:
                modelAndView.setViewName("login");
                break;
        }
        return modelAndView;
    }
}
